package com.cy.pj.ajax.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
public class ajaxController {
    private List<String> names=new ArrayList<String>();//假设这是存储数据的表

    @GetMapping("/doAjaxGet")
    public String doAjaxGet(){
        return "Ajax Get Request Result";
    }

    /**通过此方法校验名字是否存在*/
    @RequestMapping("/doCheck")
    public String doCheck(String name) {
        if(names.contains(name))
            return "名字"+name+"已经存在,请选择其它名字";
        return "名字"+name+"不存在,可以注册";
    }

    /**将客户端请求数据写入到names对应的集合*/
    @RequestMapping("/doSave")
    public String doSave(String name) {
        System.out.println("name="+name);
        names.add(name);
        System.out.println(names);
        return "save ok";
    }
}
